import os
import simplejson
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class BModel(db.Model):
    @staticmethod
    def parent_key(): 
        return db.Key.from_path('Burgerometer', 'BModel')

    def to_dict(self):
        return dict(
            [(p, unicode(getattr(self, p)))
             for p in self.properties()])

class Location(BModel):
    geo = db.GeoPtProperty()
    country = db.StringProperty()
    currency = db.StringProperty() # TODO: Should prolly be its own model.

    @staticmethod
    def create(geo, country, currency):
        return Location.get_or_insert(
            country, 
            geo=geo, 
            country=country, 
            currency=currency, 
            parent=Location.parent_key())

class Item(BModel):
    name = db.StringProperty(required=True)

    @staticmethod
    def create(name):
        return Item.get_or_insert(
            name, 
            name=name,
            parent=Item.parent_key())

class Price(BModel):
    location = db.ReferenceProperty(Location, required=True)
    item = db.ReferenceProperty(Item, required=True)
    cost = db.IntegerProperty(required=True) # This is the price in 3 decimal places.
    when = db.DateTimeProperty(auto_now_add=True)

    @staticmethod
    def create(location, item, cost):
        return Price.get_or_insert(
            location.key().name() + item.key().name(),
            location=location, item=item, cost=cost, parent=Price.parent_key())
